package ywh.utils.ui {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.EventPhase;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;

	/**
	 * 工具类
	 * 提示框类，本类用到了数据结构Dictionary;
	 * @author Willi WH Yang
	 */
	public class Tooltrip extends Sprite {
		public static var tr : Tooltrip;
		public static var tripDictionary : Dictionary;
		private var textField : TextField;
		private static var tooltripPadding : Number = 2;
		private static var tooltripColor : uint = 0xFF0000;
		private static var tooltripLineColor : uint = 0x000000;
		private static var tooltripBackgroundColor : uint = 0xcccccc;

		public function Tooltrip() {
		}

		/**
		 * 为一个显示对象添加一个提示框;
		 * @param	dp	显示对象
		 * @param	tripString 提示文字
		 */
		public static function addTrip(dp : InteractiveObject, tripString : String) : void {
			initATrip();
			dp.addEventListener(MouseEvent.MOUSE_OVER, tr.showTrip);
			dp.addEventListener(MouseEvent.MOUSE_OUT, tr.hideTrip);
			tripDictionary[dp] = tripString;
		}

		private static function initATrip() : void {
			if (!tr) {
				tr = new Tooltrip();
				tr.initTextField();
				tripDictionary = new Dictionary(true);
			}
		}

		private function initTextField() : void {
			textField = new TextField();
			textField.autoSize = TextFieldAutoSize.LEFT;
			textField.textColor = tooltripColor;
			this.addChild(textField);
		}

		private function showTrip(me : MouseEvent) : void {
			if (me.eventPhase == EventPhase.AT_TARGET) {
				var dp : DisplayObject = me.target as DisplayObject;
				var bounds : Rectangle = dp.getBounds(dp.root);
				(dp.root as DisplayObjectContainer).addChild(this);
				this.textField.text = tripDictionary[dp];
				this.textField.x = bounds.x + (bounds.width - textField.width) / 2;
				this.textField.y = bounds.y - (textField.height + 10);
				this.graphics.clear();
				this.graphics.lineStyle(1, tooltripLineColor);
				this.graphics.beginFill(tooltripBackgroundColor, 1);
				this.graphics.moveTo(textField.x - tooltripPadding, textField.y - tooltripPadding);
				this.graphics.lineTo(textField.x + textField.width + tooltripPadding, textField.y - tooltripPadding);
				this.graphics.lineTo(textField.x + textField.width + tooltripPadding, textField.y + textField.height + tooltripPadding);
				this.graphics.lineTo(textField.x + textField.width / 2 + 5, textField.y + textField.height + tooltripPadding);
				this.graphics.lineTo(textField.x + textField.width / 2, textField.y + textField.height + 10);
				this.graphics.lineTo(textField.x + textField.width / 2 - 5, textField.y + textField.height + tooltripPadding);
				this.graphics.lineTo(textField.x - tooltripPadding, textField.y + textField.height + tooltripPadding);
				this.graphics.lineTo(textField.x - tooltripPadding, textField.y - tooltripPadding);
				this.graphics.endFill();
			}
		}

		private function hideTrip(me : MouseEvent) : void {
			if (me.eventPhase == EventPhase.AT_TARGET) {
				var dp : DisplayObject = me.target as DisplayObject;
				(dp.root as DisplayObjectContainer).removeChild(this);
			}
		}
	}
}